package com.cgbl.cg.ui.work

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityNextReleaseWorkBinding
import com.cgbl.cg.view.AddTagPop
import com.cgbl.cg.view.FilterPop
import com.cgbl.cg.view.WorkMrvPop
import com.cgbl.cg.vm.work.NextReleaseWorkViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CreateWorkBean
import com.robot.base.util.LiveDataBus

@Route(path = AppConstants.Router.WORK.A_NEXT_RELEASE_WORK)
class NextReleaseWorkActivity : GLBaseActivity<AppActivityNextReleaseWorkBinding, NextReleaseWorkViewModel>() {

    var title = ""
    var direction = ""
    var list = ArrayList<String>()
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_next_release_work

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_en_tips_124))
        initListener()
    }

    override fun initData() {
        super.initData()
        viewModel.title = title
        viewModel.direction = direction
        viewModel.imgList = list
        viewModel.imgUrl.set(list[0])

        viewModel.allType()
    }
    override fun initParam() {
        super.initParam()
        title = intent.getStringExtra("title")?:""
        direction = intent.getStringExtra("direction")?:""
        list = intent.getStringArrayListExtra("list")?: arrayListOf()
    }

    fun initListener(){
        binding.switchSave.setOnCheckedChangeListener { buttonView, isChecked ->
            viewModel.save.set(isChecked)
        }
    }
    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.showTypeEvent.observe(this){
            val list = viewModel.typeList.map { it.name }
            val workMrvPop = WorkMrvPop(this,"选择作品类型",
                viewModel.typeFirstText.get()?:"", list as ArrayList<String>
            )
            XPopup.Builder(this)
                .asCustom(workMrvPop)
                .show()

            workMrvPop.setConfirmClickListener {
                viewModel.typeFirstText.set(viewModel.typeList[it].name)
                val courseAllTypeBean = viewModel.typeList[it]

                val childrenList = courseAllTypeBean.childrenList?.map { it.name }

                if(childrenList.isNullOrEmpty()){
                    workMrvPop.dismiss()
                    viewModel.typeText.set(viewModel.typeList[it].name)
                    viewModel.type = viewModel.typeList[it].id?:""
                    return@setConfirmClickListener
                }
                val workMrvChildrenPop = WorkMrvPop(this,"选择作品类型",
                    viewModel.typeText.get()?:"", childrenList as ArrayList<String>
                )
                XPopup.Builder(this)
                    .asCustom(workMrvChildrenPop)
                    .show()

                workMrvChildrenPop.setConfirmClickListener {it1->
                    workMrvChildrenPop.dismiss()
                    workMrvPop.dismiss()
                    viewModel.typeText.set(viewModel.typeList[it].childrenList?.get(it1)?.name)
                    viewModel.type = viewModel.typeList[it].childrenList?.get(it1)?.id?:""
                }
            }
        }

        viewModel.uc.showDeclareEvent.observe(this){
            val workMrvPop = WorkMrvPop(this,"创作声明",viewModel.declareText.get()?:"", viewModel.declareList)
            XPopup.Builder(this)
                .asCustom(workMrvPop)
                .show()
            workMrvPop.setConfirmClickListener {
                workMrvPop.dismiss()
                viewModel.declareText.set(viewModel.declareList[it])
                viewModel.declare = "${it}"
            }
        }

        viewModel.uc.showCopyrightTypeEvent.observe(this){
            val workMrvPop = WorkMrvPop(this,"版权",viewModel.copyrightTypeText.get()?:"", viewModel.copyrightTypeList)
            XPopup.Builder(this)
                .asCustom(workMrvPop)
                .show()

            workMrvPop.setConfirmClickListener {
                workMrvPop.dismiss()
                viewModel.copyrightTypeText.set(viewModel.copyrightTypeList[it])
                viewModel.copyrightType = "${it}"
            }
        }

        viewModel.uc.showTagEvent.observe(this){
            val addTagPop = AddTagPop(this, viewModel.tagList)
            XPopup.Builder(this)
                .moveUpToKeyboard(false)
                .asCustom(addTagPop)
                .show()

            addTagPop.setConfirmClickListener { it ->
                addTagPop.dismiss()
                viewModel.tagList = it
                val stringBuffer = StringBuffer()
                viewModel.tagList.forEach { it1->
                    stringBuffer.append(it1).append(",")
                }
                if(!stringBuffer.isNullOrEmpty()){
                    stringBuffer.deleteCharAt(stringBuffer.length-1)
                }
                viewModel.tagText.set(stringBuffer.toString())
            }
        }

    }

    override fun onBackPressed() {
        val bean = CreateWorkBean()
        bean.type = viewModel.type
        bean.imgUrl = viewModel.imgUrl.get()?:""
        bean.save = viewModel.save.get()?:false
        bean.tagList = viewModel.tagList
        bean.declare = viewModel.declare
        bean.copyrightType = viewModel.copyrightType
        LiveDataBus.get().with("WorkData", CreateWorkBean::class.java)
            .postValue(bean)
        super.onBackPressed()
    }
}